using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using UnityEngine;

namespace QFramework.Adventure
{
    public class IsDoneManager : MonoBehaviour
    {

        public List<IIsDone> isDones;//

       
        public ResLoader mResLoader;


        private void Awake()
        {
          
            mResLoader = null;
            mResLoader = ResLoader.Allocate();
            isDones = new List<IIsDone>();



            MessageCenter.Register(MessageEnum.SpwenEnemy,SpwenEnemyFun);
            MessageCenter.Register(MessageEnum.SaveData, SaveDataFun);
            MessageCenter.Register(MessageEnum.CheckVictory, GameVictoryFun);

        }

        private void SpwenEnemyFun()
        {
            if (GameData.CurrentAddSceneName == QAssetBundle.Level01_unity.LEVEL01) { 

            var boarPos = new List<Vector3>()
            {
                new Vector3 (37.46151f,8.01f,0),
                new Vector3 (47.54034f,-0.80f,0),

            };
            var snailPos = new List<Vector3>()
            {

                new Vector3 (5.965165f,7.994f,0),
            };


            for (int i = 0; i < boarPos.Count; i++)
            {
                string loadName = GameConfig.Item_Boar;
                var item = mResLoader.LoadSync<GameObject>(loadName).Name(loadName + "_0" + (i));

                item.Instantiate().transform.LocalPosition(boarPos[i]).Parent(transform).GetComponent<FSMBaseObj>().Init();

            }

            for (int i = 0; i < snailPos.Count; i++)
            {
                string loadName = GameConfig.Item_Snail;
                var item = mResLoader.LoadSync<GameObject>(loadName).Name(loadName + "_0" + (i));

                item.Instantiate().transform.LocalPosition(snailPos[i]).Parent(transform).GetComponent<FSMBaseObj>().Init();

            }

            }
            if (GameData.CurrentAddSceneName == QAssetBundle.Level02_unity.LEVEL02)
            {

                var beePos = new List<Vector3>()
                {
                    new Vector3 (11.17638f,11.84701f,0),
                    new Vector3 (29.39f,11.22f,0),
                    new Vector3 (45.65f,8.13f,0),


                };
                var boarPos = new List<Vector3>() 
                { 
                    new Vector3(30.39012f, -0.7955346f, 0), 
                };
           

                for (int i = 0; i < beePos.Count; i++)
                {
                    string loadName = GameConfig.Item_Bee;
                    var item = mResLoader.LoadSync<GameObject>(loadName).Name(loadName + "_0" + (i));

                    item.Instantiate().transform.LocalPosition(beePos[i]).Parent(transform).GetComponent<FSMBaseObj>().Init();

                }

                for (int i = 0; i < boarPos.Count; i++)
                {
                    string loadName = GameConfig.Item_Boar;
                    var item = mResLoader.LoadSync<GameObject>(loadName).Name(loadName + "_0" + (i));

                    item.Instantiate().transform.LocalPosition(boarPos[i]).Parent(transform).GetComponent<FSMBaseObj>().Init();

                }

            }


            for (int i = 0; i < transform.childCount; i++)
            {
                isDones.Add(transform.GetChild(i).GetComponent<IIsDone>());
            }

            for (int i = 0; i < isDones.Count; i++)
            {
                var mKey = GameData.CurrentAddSceneName + transform.GetChild(i).name;

                if (GameData.storageData.itemState.GetValueOrDefault(mKey))
                    isDones[i].isDone = GameData.storageData.itemState.GetValueOrDefault(mKey);
                   isDones[i].SelfObj.SetActive(!isDones[i].isDone);
            }
        }

        private void Update()
        {

            if (Input.GetKeyDown(KeyCode.T))
            {
                UIKit.OpenPanel<GameVictoryrPanel>();
            }


        }

    
        void GameVictoryFun()
        {
            
            if (  GameData.CurrentAddSceneName == QAssetBundle.Level02_unity.LEVEL02)
            {

                if(GameObject.FindGameObjectsWithTag("Enemy").Length==0) UIKit.OpenPanel<GameVictoryrPanel>(); 
            }
        }

        private void OnDestroy()
        {
            MessageCenter.UnRegister(MessageEnum.SpwenEnemy, SpwenEnemyFun);
            MessageCenter.UnRegister(MessageEnum.SaveData, SaveDataFun); 
            MessageCenter.UnRegister(MessageEnum.CheckVictory, GameVictoryFun);

            mResLoader.Recycle2Cache();
            mResLoader = null;

        }
        private void SaveDataFun()
        {
            for (int i = 0; i < isDones.Count; i++)
            {
                var mKey = GameData.CurrentAddSceneName + transform.GetChild(i).name;

                if (GameData.storageData.itemState.ContainsKey(mKey))
                    GameData.storageData.itemState[mKey] = isDones[i].isDone;
                else
                    GameData.storageData.itemState.Add(mKey, isDones[i].isDone);
            }
          
        }

     
    }
}